﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCommunity.Data;

namespace XCommunity.Members
{
  public class MemberService
  {

    private AccountStaticsDataProvider _dataProvider = new AccountStaticsDataProvider();


    public MemberService( AccountService accountService, ICommunityContext context )
    {
      _accountService = accountService;
      _context = context;
      _cache = new MemberCache( this );
    }


    private MemberCache _cache;

    public MemberCache Cache
    {
      get { return _cache; }
    }


    private AccountService _accountService;

    public AccountService AccountService
    {
      get { return _accountService; }
    }


    private ICommunityContext _context;

    public ICommunityContext Context
    {
      get { return _context; }
    }


    /// <summary>
    /// 获取成员对象
    /// </summary>
    /// <param name="username">用户名</param>
    /// <returns>指定用户的成员对象</returns>
    public virtual Member GetMember( string username )
    {
      Member member = Cache.GetMember( username );
      if ( member == null )
      {
        member = CreateMember( username );
        Cache.AddMember( member );
      }

      return member;
    }


    public virtual Member CreateMember( string username )
    {
      return new Member( username )
      {
        NickName = username,
        Documents = _dataProvider.GetDocuments( username ),
        Topics = _dataProvider.GetTopics( username )
      };
    }


    public void Init()
    {
      _cache.Init( Context.CacheService );
    }
  }
}
